url = 'https://www.acwing.com/problem/content/3959/'

n = int(input())

data = list(map(int,input().split()))
total = sum(data)
res = s1 = s2 = 0
if total % 3 == 0:
    for i in data[:-1]:
        s1 += i
        if s1 == 2 * total // 3:
            res += s2
        if s1 == total // 3:
            s2 += 1
    print(res)
else:
    print(0)